package com.bigseller.study;


import freemarker.core.Environment;
import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapperBuilder;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateDirectiveModel;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;

import java.io.IOException;
import java.util.Map;

public class CustomTagDirective implements TemplateDirectiveModel {
    private static final String METHOD_KEY = "method";


    @Override
    public void execute(Environment environment, Map map, TemplateModel[] templateModels, TemplateDirectiveBody templateDirectiveBody) throws TemplateException, IOException, IOException, TemplateException {
        if (map.containsKey(METHOD_KEY)) {
			DefaultObjectWrapperBuilder builder = new DefaultObjectWrapperBuilder(Configuration.VERSION_2_3_20);
            String method = map.get(METHOD_KEY).toString();
            switch (method) {
                case "barcode":
					// 将数据对象转换成对应的TemplateModel
                    TemplateModel tm = builder.build().wrap("xxxxxxx");
                    environment.setVariable("tagsList", tm);
                    break;
                default:
                    break;
            }
        }
        templateDirectiveBody.render(environment.getOut());
    }
}
